package cn.stylefeng.guns.modular.lx.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.lx.entity.LxDdWzh;
import cn.stylefeng.guns.modular.lx.mapper.LxDdWzhMapper;
import cn.stylefeng.guns.modular.lx.mapper.LxSpWzhMapper;
import cn.stylefeng.guns.modular.lx.model.params.LxDdWzhParam;
import cn.stylefeng.guns.modular.lx.model.params.LxSpWzhParam;
import cn.stylefeng.guns.modular.lx.model.result.LxDdWzhResult;
import  cn.stylefeng.guns.modular.lx.service.LxDdWzhService;
import cn.stylefeng.guns.modular.lx.service.LxSpWzhService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzh
 * @since 2023-12-06
 */
@Service
public class LxDdWzhServiceImpl extends ServiceImpl<LxDdWzhMapper, LxDdWzh> implements LxDdWzhService {

    @Autowired
    private LxSpWzhService lxSpWzhService;
    @Override
    public void add(LxDdWzhParam param){
        LxDdWzh entity = getEntity(param);
        lxSpWzhService.update2(param);
        entity.setDdZj(Double.parseDouble(entity.getDdSl()) * entity.getDdZj());

        this.save(entity);
    }


    @Override
    public void add2(LxDdWzhParam param) {
        LxDdWzh entity = getEntity(param);
        lxSpWzhService.update2(param);
        entity.setDdZj(Double.parseDouble(entity.getDdSl()) * entity.getDdZj());
        this.save(entity);
    }

    @Override
    public void delete(LxDdWzhParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(LxDdWzhParam param){
        LxDdWzh oldEntity = getOldEntity(param);
        LxDdWzh newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public LxDdWzhResult findBySpec(LxDdWzhParam param){
        return null;
    }

    @Override
    public List<LxDdWzhResult> findListBySpec(LxDdWzhParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(LxDdWzhParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(LxDdWzhParam param){
        return param.getDdId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private LxDdWzh getOldEntity(LxDdWzhParam param) {
        return this.getById(getKey(param));
    }

    private LxDdWzh getEntity(LxDdWzhParam param) {
        LxDdWzh entity = new LxDdWzh();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
